package middleware

import (
	"strings"

	"192.168.1.75/go-pkg/logx"
	"github.com/gin-gonic/gin"
)

func InjectTraceID() gin.HandlerFunc {
	return func(c *gin.Context) {
		spanctx := logx.Start(c.Request.Context(), "system-admin")
		defer logx.End(spanctx)
		// 通过header输出trace-id的值
		c.Header("trace-id", logx.TraceID(spanctx))
		// 增加B3 Header，用于链路追踪
		c.Request.Header.Set("B3", strings.Join([]string{logx.TraceID(spanctx), logx.SpanID(spanctx), "1"}, "-"))
	}
}
